<template>
  <view class="container">
    <view class="header">
      <view class="header-content">
        <view class="header-title">预约信息列表</view>
        <view class="header-subtitle">共 {{ sortedAppointmentList.length }} 条预约记录</view>
        <view class="weather-icons">
          <text class="iconfont icon-sun"></text>
          <text class="iconfont icon-cloud"></text>
          <text class="iconfont icon-rain"></text>
        </view>
      </view>
    </view>
    
    <view class="content">
      <view v-if="isLoading" class="loading">
        <text class="iconfont icon-spinner"></text> 加载中...
      </view>
      
      <view v-else-if="sortedAppointmentList.length === 0" class="empty">
        <text class="iconfont icon-empty"></text>
        <view>暂无预约记录</view>
      </view>
      
      <view v-else class="appointment-list">
        <view 
          v-for="(item, index) in sortedAppointmentList" 
          :key="item.id" 
          class="appointment-item"
        >
          <view class="item-header">
            <view class="order-number">预约编号: {{ sortedAppointmentList.length - index }}</view>
            <view class="appointment-date">{{ getOrderDate(item.orderTime) }}</view>
          </view>
          
          <view class="item-content">      
            <view class="info-row">
              <text class="label">姓名:</text>
              <text class="value">{{ item.name || '未填写' }}</text>
            </view>
            
            <view class="info-row">
              <text class="label">单位:</text>
              <text class="value">{{ item.unit || '未填写' }}</text>
            </view>
            
            <view class="info-row">
              <text class="label">联系电话:</text>
              <text class="value">{{ item.phoneNum || '未填写' }}</text>
            </view>
            
            <view class="info-row">
              <text class="label">预约时间:</text>
              <text class="value">{{ item.orderTime }}</text>
            </view>
          </view>
          
          <view class="item-footer">
            <view class="update-time">用户提交时间: {{ item.updateTime }}</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';

// 数据定义
const appointmentList = ref([]);
const isLoading = ref(true);

// 按创建时间排序的计算属性（最新的在前面）
const sortedAppointmentList = computed(() => {
  return [...appointmentList.value].sort((a, b) => {
    return new Date(b.createTime) - new Date(a.createTime);
  });
});

// 获取预约列表
const fetchAppointmentList = async () => {
  isLoading.value = true;
  
  try {
    const response = await uni.request({
      url: 'http://218.28.7.243:32201/api/science/lastRecords',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      }
    });
    
    if (response.statusCode === 200 && response.data.success) {
      appointmentList.value = response.data.data;
    } else {
      console.error('获取预约列表失败:', response);
      uni.showToast({
        title: '获取数据失败',
        icon: 'none'
      });
    }
  } catch (error) {
    console.error('请求预约列表时发生错误:', error);
    uni.showToast({
      title: '网络请求失败',
      icon: 'none'
    });
  } finally {
    isLoading.value = false;
  }
};

// 获取预约日期 (YYYY年MM月DD日)
const getOrderDate = (dateTimeStr) => {
  if (!dateTimeStr) return '';
  const date = new Date(dateTimeStr);
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  return `${year}年${month}月${day}日`;
};

// 页面加载时获取数据
onMounted(() => {
  fetchAppointmentList();
});

// 下拉刷新
const onPullDownRefresh = () => {
  fetchAppointmentList().finally(() => {
    uni.stopPullDownRefresh();
  });
};
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.header {
  background: #4a6bdf;
  color: white;
  padding: 40rpx 30rpx;
  text-align: center;
  position: relative;
  overflow: hidden;
  
  .header-content {
    position: relative;
    z-index: 2;
  }
  
  .header-title {
    font-size: 36rpx;
    font-weight: 500;
    margin-bottom: 10rpx;
  }
  
  .header-subtitle {
    font-size: 28rpx;
    opacity: 0.9;
    margin-bottom: 20rpx;
  }
  
  .weather-icons {
    display: flex;
    justify-content: center;
    gap: 30rpx;
    
    .iconfont {
      font-size: 40rpx;
      opacity: 0.7;
      animation: float 3s ease-in-out infinite;
      
      &:nth-child(2) {
        animation-delay: 0.5s;
      }
      
      &:nth-child(3) {
        animation-delay: 1s;
      }
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10rpx);
  }
}

.content {
  padding: 20rpx;
}

.loading {
  text-align: center;
  padding: 60rpx 0;
  color: #666;
  
  .iconfont {
    font-size: 40rpx;
    margin-right: 10rpx;
  }
}

.empty {
  text-align: center;
  padding: 100rpx 0;
  color: #999;
  
  .iconfont {
    font-size: 80rpx;
    margin-bottom: 20rpx;
  }
}

.appointment-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.appointment-item {
  background: white;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24rpx 30rpx;
  background: #f8f9fd;
  border-bottom: 1rpx solid #eaeef2;
  
  .order-number {
    font-size: 28rpx;
    font-weight: 500;
    color: #333;
  }
  
  .appointment-date {
    font-size: 24rpx;
    color: #666;
    background: #e3f2fd;
    padding: 6rpx 12rpx;
    border-radius: 6rpx;
    font-weight: 500;
    white-space: nowrap;
  }
}

.item-content {
  padding: 30rpx;
}

.info-row {
  display: flex;
  margin-bottom: 20rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .label {
    width: 160rpx;
    font-size: 26rpx;
    color: #666;
    flex-shrink: 0;
  }
  
  .value {
    flex: 1;
    font-size: 26rpx;
    color: #333;
    word-break: break-all;
  }
}

.item-footer {
  padding: 20rpx 30rpx;
  background: #fafbff;
  border-top: 1rpx solid #eaeef2;
  font-size: 22rpx;
  color: #999;
  
  .update-time {
    text-align: right;
  }
}

// 响应式设计
@media (max-width: 768rpx) {
  .info-row {
    flex-direction: column;
    gap: 8rpx;
    
    .label {
      width: auto;
      font-weight: 500;
    }
  }
  
  .item-header {
    flex-direction: row;
    justify-content: space-between;
  }
  
  .item-footer {
    text-align: left;
  }
}
</style>